package Question1_5; import CareerCupLibrary.AssortedMethods; public class Question { // Assume string has sufficient free space at the end public static void ReplaceFun(char[] str, int length) { int spaceCount = 0, newLength, i = 0; for (i = 0; i < length; i++) { if (str[i] == ' ') { spaceCount++; } } newLength = length + spaceCount * 2; str[newLength] = '\0'; for (i = length - 1; i >= 0; i--) { if (str[i] == ' ') { str[newLength - 1] = '0'; str[newLength - 2] = '2'; str[newLength - 3] = '%'; newLength = newLength - 3; } else { str[newLength - 1] = str[i]; newLength = newLength - 1; } } } public static void main(String[] args) { String str = "abc d e f"; char[] arr = new char[str.length() + 3 * 2 + 1]; for (int i = 0; i < str.length(); i++) { arr[i] = str.charAt(i); } ReplaceFun(arr, str.length()); System.out.println(AssortedMethods.charArrayToString(arr)); } }